﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

namespace Teigha.Core
{
	public class Helpers
	{
		private static Helpers.RxObjectDynamicCreatorDelegate odrxFindCreatorDelegate(IntPtr isa, IntPtr p)
		{
			Helpers.RxObjectDynamicCreatorDelegate rxObjectDynamicCreatorDelegate;
			if (Helpers.constructorMap.TryGetValue(isa, out rxObjectDynamicCreatorDelegate))
			{
				return rxObjectDynamicCreatorDelegate;
			}
			Helpers.RxObjectDynamicCreatorDelegate result;
			lock (Helpers.constructorMap)
			{
				if (Helpers.constructorMap.TryGetValue(isa, out rxObjectDynamicCreatorDelegate))
				{
					result = rxObjectDynamicCreatorDelegate;
				}
				else
				{
					string text = GlobalsPINVOKE.OdRxClass_name(new HandleRef(null, isa));
					Type type = Type.GetType("Teigha.Core." + string.Format("{0}_Internal", text) + ",YJKAcadInterfaceCore");
					if (type == null)
					{
						type = Type.GetType("Teigha.Core." + text + ",YJKAcadInterfaceCore");
					}
					if (type == null)
					{
						type = Type.GetType("Teigha.TG." + string.Format("{0}_Internal", text) + ",TG_SwigDbMgd");
					}
					if (type == null)
					{
						type = Type.GetType("Teigha.TG." + text + ",TG_SwigDbMgd");
					}
					if (type == null)
					{
						type = Type.GetType("Teigha.TD." + string.Format("{0}_Internal", text) + ",YJKAcadInterfaceDataBase");
					}
					if (type == null)
					{
						type = Type.GetType("Teigha.TD." + text + ",YJKAcadInterfaceDataBase");
					}
					if (type == null && text[0] == 'A' && text[1] == 'c')
					{
						text = "Od" + text.Substring(2);
						type = Type.GetType("Teigha.TD." + text + ",YJKAcadInterfaceDataBase");
					}
					if (type == null)
					{
						IntPtr intPtr = GlobalsPINVOKE.OdRxClass_myParent(new HandleRef(null, isa));
						if (intPtr == IntPtr.Zero)
						{
							result = null;
						}
						else
						{
							result = Helpers.odrxFindCreatorDelegate(intPtr, IntPtr.Zero);
						}
					}
					else
					{
						ConstructorInfo constructor = type.GetConstructor(new Type[]
						{
							typeof(IntPtr),
							typeof(bool)
						});
						DynamicMethod dynamicMethod = new DynamicMethod("CreateObject_" + text, typeof(OdRxObject), new Type[]
						{
							typeof(IntPtr),
							typeof(bool)
						});
						ILGenerator ilgenerator = dynamicMethod.GetILGenerator();
						ilgenerator.Emit(OpCodes.Ldarg_0);
						ilgenerator.Emit(OpCodes.Ldarg_1);
						ilgenerator.Emit(OpCodes.Newobj, constructor);
						ilgenerator.Emit(OpCodes.Castclass, typeof(OdRxObject));
						ilgenerator.Emit(OpCodes.Ret);
						Helpers.RxObjectDynamicCreatorDelegate rxObjectDynamicCreatorDelegate2 = (Helpers.RxObjectDynamicCreatorDelegate)dynamicMethod.CreateDelegate(typeof(Helpers.RxObjectDynamicCreatorDelegate));
						Helpers.constructorMap.Add(isa, rxObjectDynamicCreatorDelegate2);
						result = rxObjectDynamicCreatorDelegate2;
					}
				}
			}
			return result;
		}

		public static void odUninit()
		{
			Helpers.constructorMap.Clear();
		}

		public static OdRxObject odrxCreateObjectInternal(IntPtr p, bool own)
		{
			if (p == IntPtr.Zero)
			{
				return null;
			}
			IntPtr isa = GlobalsPINVOKE.OdRxObject_isA(new HandleRef(null, p));
			Helpers.RxObjectDynamicCreatorDelegate rxObjectDynamicCreatorDelegate = Helpers.odrxFindCreatorDelegate(isa, p);
			if (rxObjectDynamicCreatorDelegate == null)
			{
				return new OdRxObject(p, own);
			}
			return rxObjectDynamicCreatorDelegate(p, own);
		}

		private static int copyBytes(IntPtr data, int N, byte[] bb)
		{
			if (bb != null)
			{
				foreach (byte val in bb)
				{
					Marshal.WriteByte(data, N++, val);
				}
			}
			return N;
		}

		public static IntPtr MarshalClipRegion(OdGePoint2dArray[] contours)
		{
			int num = 4 + 4 * contours.Length;
			foreach (OdGePoint2dArray odGePoint2dArray in contours)
			{
				num += odGePoint2dArray.Count * 2 * 8;
			}
			IntPtr intPtr = Marshal.AllocCoTaskMem(num);
			Marshal.WriteInt32(intPtr, contours.Length);
			int num2 = 4;
			foreach (OdGePoint2dArray odGePoint2dArray2 in contours)
			{
				Marshal.WriteInt32(intPtr, num2, odGePoint2dArray2.Count);
				num2 += 4;
			}
			for (int k = 0; k < contours.Length; k++)
			{
				for (int l = 0; l < contours[k].Count; l++)
				{
					num2 = Helpers.copyBytes(intPtr, num2, BitConverter.GetBytes(contours[k][l].x));
					num2 = Helpers.copyBytes(intPtr, num2, BitConverter.GetBytes(contours[k][l].y));
				}
			}
			return intPtr;
		}

		public static OdGePoint2dArray[] UnMarshalClipRegion(IntPtr pts)
		{
			byte[] array = new byte[4];
			Marshal.Copy(pts, array, 0, 4);
			int num = BitConverter.ToInt32(array, 0);
			byte[] value = new byte[num * 4];
			Marshal.Copy(pts, array, 4, 4 * num);
			int[] array2 = new int[num];
			int num2 = 0;
			int num3 = 0;
			for (int i = 0; i < num; i++)
			{
				array2[i] = BitConverter.ToInt32(value, num2);
				num2 += 4;
				num3 += array2[i];
			}
			byte[] array3 = new byte[16 * num3];
			Marshal.Copy(pts, array3, 4 + 4 * num, 16 * num3);
			OdGePoint2dArray[] array4 = new OdGePoint2dArray[num];
			num2 = 0;
			for (int j = 0; j < num; j++)
			{
				for (int k = 0; k < array2[j]; k++)
				{
					double xx = (double)BitConverter.ToInt32(array3, num2);
					num2 += 8;
					double yy = (double)BitConverter.ToInt32(array3, num2);
					num2 += 8;
					array4[j].Add(new OdGePoint2d(xx, yy));
				}
			}
			return array4;
		}

		private static int copyBytes(IntPtr data, int N, bool[] arr)
		{
			if (arr != null)
			{
				for (int i = 0; i < arr.Length; i++)
				{
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(arr[i]));
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, OdGsDCPoint[] pts)
		{
			if (pts != null)
			{
				for (int i = 0; i < pts.Length; i++)
				{
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].x));
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].y));
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, OdGePoint3d[] pts)
		{
			if (pts != null)
			{
				for (int i = 0; i < pts.Length; i++)
				{
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].x));
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].y));
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].z));
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, OdGePoint2d[] pts)
		{
			if (pts != null)
			{
				for (int i = 0; i < pts.Length; i++)
				{
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].x));
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].y));
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, OdGeVector3d[] pts)
		{
			if (pts != null)
			{
				for (int i = 0; i < pts.Length; i++)
				{
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].x));
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].y));
					N = Helpers.copyBytes(data, N, BitConverter.GetBytes(pts[i].z));
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, OdCmEntityColor[] cc)
		{
			if (cc != null)
			{
				for (int i = 0; i < cc.Length; i++)
				{
					Marshal.WriteInt32(data, N, (int)cc[i].color());
					N += 4;
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, int[] cc)
		{
			if (cc != null)
			{
				for (int i = 0; i < cc.Length; i++)
				{
					Marshal.WriteInt32(data, N, cc[i]);
					N += 4;
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, ushort[] cc)
		{
			if (cc != null)
			{
				for (int i = 0; i < cc.Length; i++)
				{
					Marshal.WriteInt16(data, N, (short)cc[i]);
					N += 2;
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, OdDbStub[] cc)
		{
			if (cc != null)
			{
				for (int i = 0; i < cc.Length; i++)
				{
					IntPtr handle = OdDbStub.getCPtr(cc[i]).Handle;
					Marshal.WriteIntPtr(data, N, handle);
					N += Marshal.SizeOf(handle);
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, OdGiMapper[] cc)
		{
			if (cc != null)
			{
				for (int i = 0; i < cc.Length; i++)
				{
					IntPtr handle = OdGiMapper.getCPtr(cc[i]).Handle;
					Marshal.WriteIntPtr(data, N, handle);
					N += Marshal.SizeOf(handle);
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, OdCmTransparency[] cc)
		{
			if (cc != null)
			{
				for (int i = 0; i < cc.Length; i++)
				{
					IntPtr handle = OdCmTransparency.getCPtr(cc[i]).Handle;
					Marshal.WriteIntPtr(data, N, handle);
					N += Marshal.SizeOf(handle);
				}
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, IntPtr[] cc)
		{
			if (cc != null)
			{
				for (int i = 0; i < cc.Length; i++)
				{
					Marshal.WriteIntPtr(data, N, cc[i]);
					N += Marshal.SizeOf(cc[i]);
				}
			}
			return N;
		}

		public static IntPtr MarshalPointPair(OdGePoint3d[] pts)
		{
			return Helpers.MarshalPointFixedArray(pts, 2);
		}

		public static OdGePoint3d[] UnMarshalPointPair(IntPtr pts)
		{
			return Helpers.UnMarshalPointFixedArray(pts, 2);
		}

		public static IntPtr MarshalPoint2dPair(OdGePoint2d[] pts)
		{
			return Helpers.MarshalPoint2dFixedArray(pts, 2);
		}

		public static OdGePoint2d[] UnMarshalPoint2dPair(IntPtr pts)
		{
			return Helpers.UnMarshalPoint2dFixedArray(pts, 2);
		}

		public static IntPtr MarshalOdGsDCPointArray(OdGsDCPoint[] pts)
		{
			int cb = 4 + pts.Length * 2 * 4;
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			Marshal.WriteInt32(intPtr, pts.Length);
			Helpers.copyBytes(intPtr, 4, pts);
			return intPtr;
		}

		public static OdGsDCPoint[] UnMarshalOdGsDCPointArray(IntPtr pts)
		{
			int num = Marshal.ReadInt32(pts);
			int num2 = num * 2 * 4 + 4;
			byte[] array = new byte[num2];
			Marshal.Copy(pts, array, 0, num2);
			OdGsDCPoint[] array2 = new OdGsDCPoint[num];
			int num3 = 4;
			for (int i = 0; i < num; i++)
			{
				int xx = BitConverter.ToInt32(array, num3);
				num3 += 4;
				int yy = BitConverter.ToInt32(array, num3);
				num3 += 4;
				array2[i] = new OdGsDCPoint(xx, yy);
			}
			Marshal.FreeCoTaskMem(pts);
			return array2;
		}

		public static IntPtr MarshalPoint3dArray(OdGePoint3d[] pts)
		{
			int cb = 4 + pts.Length * 3 * 8;
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			Marshal.WriteInt32(intPtr, pts.Length);
			Helpers.copyBytes(intPtr, 4, pts);
			return intPtr;
		}

		public static OdGePoint3d[] UnMarshalPoint3dArray(IntPtr pts)
		{
			int num = Marshal.ReadInt32(pts);
			int num2 = num * 3 * 8 + 4;
			byte[] array = new byte[num2];
			Marshal.Copy(pts, array, 0, num2);
			OdGePoint3d[] array2 = new OdGePoint3d[num];
			int num3 = 4;
			for (int i = 0; i < num; i++)
			{
				double xx = BitConverter.ToDouble(array, num3);
				num3 += 8;
				double yy = BitConverter.ToDouble(array, num3);
				num3 += 8;
				double zz = BitConverter.ToDouble(array, num3);
				num3 += 8;
				array2[i] = new OdGePoint3d(xx, yy, zz);
			}
			Marshal.FreeCoTaskMem(pts);
			return array2;
		}

		public static IntPtr MarshalPoint2dArray(OdGePoint2d[] pts)
		{
			int cb = 4 + pts.Length * 2 * 8;
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			Marshal.WriteInt32(intPtr, pts.Length);
			Helpers.copyBytes(intPtr, 4, pts);
			return intPtr;
		}

		public static OdGePoint2d[] UnMarshalPoint2dArray(IntPtr pts)
		{
			int num = Marshal.ReadInt32(pts);
			int num2 = num * 2 * 8 + 4;
			byte[] array = new byte[num2];
			Marshal.Copy(pts, array, 0, num2);
			OdGePoint2d[] array2 = new OdGePoint2d[num];
			int num3 = 4;
			for (int i = 0; i < num; i++)
			{
				double xx = BitConverter.ToDouble(array, num3);
				num3 += 8;
				double yy = BitConverter.ToDouble(array, num3);
				num3 += 8;
				array2[i] = new OdGePoint2d(xx, yy);
			}
			Marshal.FreeCoTaskMem(pts);
			return array2;
		}

		private static void getVertextDataFlags(VertexData vd, int VertexCount, ref Helpers.VertexDataFields dataAvailable, ref int Len)
		{
			if (vd != null)
			{
				dataAvailable |= Helpers.VertexDataFields.HasVertexData;
				Len += 4;
				if (vd.Normals != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasVertexNormals;
					Len += VertexCount * 3 * 8;
				}
				if (vd.TrueColors != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasVertexTrueColors;
					Len += VertexCount * Marshal.SizeOf(IntPtr.Zero);
				}
			}
		}

		private static void getEdgeDataFlags(EdgeData ed, int EdgeCount, ref Helpers.VertexDataFields dataAvailable, ref int Len)
		{
			if (ed != null)
			{
				dataAvailable |= Helpers.VertexDataFields.HasEdgeData;
				if (ed.Colors != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasEdgeColors;
					Len += EdgeCount * 2;
				}
				if (ed.LayerIds != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasEdgeLayerIds;
					Len += EdgeCount * Marshal.SizeOf(IntPtr.Zero);
				}
				if (ed.LinetypeIds != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasEdgeLinetypeIds;
					Len += EdgeCount * Marshal.SizeOf(IntPtr.Zero);
				}
				if (ed.SelectionMarkers != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasEdgeSelectionMarkers;
					Len += EdgeCount * Marshal.SizeOf(IntPtr.Zero);
				}
				if (ed.TrueColors != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasEdgeTrueColors;
					Len += EdgeCount * 4;
				}
				if (ed.Visibilities != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasEdgeVisibilities;
					Len += EdgeCount;
				}
			}
		}

		private static void getFaceDataFlags(FaceData fd, int FaceCount, ref Helpers.VertexDataFields dataAvailable, ref int Len)
		{
			if (fd != null)
			{
				dataAvailable |= Helpers.VertexDataFields.HasFaceData;
				if (fd.Colors != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasFaceColors;
					Len += FaceCount * 2;
				}
				if (fd.LayerIds != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasFaceLayerIds;
					Len += FaceCount * 2;
				}
				if (fd.Mappers != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasFaceMappers;
					Len += FaceCount * Marshal.SizeOf(IntPtr.Zero);
				}
				if (fd.MaterialIds != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasFaceMaterialIds;
					Len += FaceCount * Marshal.SizeOf(IntPtr.Zero);
				}
				if (fd.Normals != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasFaceNormals;
					Len += FaceCount * 3 * 8;
				}
				if (fd.SelectionMarkers != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasFaceSelectionMarkers;
					Len += FaceCount * Marshal.SizeOf(IntPtr.Zero);
				}
				if (fd.Transparency != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasFaceTransparency;
					Len += FaceCount * Marshal.SizeOf(IntPtr.Zero);
				}
				if (fd.TrueColors != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasFaceTrueColors;
					Len += FaceCount * 4;
				}
				if (fd.Visibilities != null)
				{
					dataAvailable |= Helpers.VertexDataFields.HasFaceVisibilities;
					Len += FaceCount;
				}
			}
		}

		private static int copyBytes(IntPtr data, int N, VertexData vd)
		{
			if (vd != null)
			{
				Marshal.WriteInt32(data, N, (int)vd.OrientationFlag);
				N += 4;
				N = Helpers.copyBytes(data, N, vd.Normals);
				N = Helpers.copyBytes(data, N, vd.TrueColors);
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, EdgeData ed)
		{
			if (ed != null)
			{
				N = Helpers.copyBytes(data, N, ed.Colors);
				N = Helpers.copyBytes(data, N, ed.LayerIds);
				N = Helpers.copyBytes(data, N, ed.LinetypeIds);
				N = Helpers.copyBytes(data, N, ed.SelectionMarkers);
				N = Helpers.copyBytes(data, N, ed.TrueColors);
				N = Helpers.copyBytes(data, N, ed.Visibilities);
			}
			return N;
		}

		private static int copyBytes(IntPtr data, int N, FaceData fd)
		{
			if (fd != null)
			{
				N = Helpers.copyBytes(data, N, fd.Colors);
				N = Helpers.copyBytes(data, N, fd.LayerIds);
				N = Helpers.copyBytes(data, N, fd.Mappers);
				N = Helpers.copyBytes(data, N, fd.MaterialIds);
				N = Helpers.copyBytes(data, N, fd.Normals);
				N = Helpers.copyBytes(data, N, fd.SelectionMarkers);
				N = Helpers.copyBytes(data, N, fd.Transparency);
				N = Helpers.copyBytes(data, N, fd.TrueColors);
				N = Helpers.copyBytes(data, N, fd.Visibilities);
			}
			return N;
		}

		public static IntPtr MarshalShellFacesData(ShellFacesData shellfaces)
		{
			int num = shellfaces.FaceList.Length;
			int edgeCount = num;
			int faceCount = num;
			int num2 = 8;
			num2 += 4 * num;
			Helpers.VertexDataFields val = (Helpers.VertexDataFields)0;
			Helpers.getEdgeDataFlags(shellfaces.EdgeData, edgeCount, ref val, ref num2);
			Helpers.getFaceDataFlags(shellfaces.FaceData, faceCount, ref val, ref num2);
			IntPtr intPtr = Marshal.AllocCoTaskMem(num2);
			Marshal.WriteInt32(intPtr, num);
			int num3 = 4;
			Marshal.WriteInt32(intPtr, num3, (int)val);
			num3 += 4;
			num3 = Helpers.copyBytes(intPtr, num3, shellfaces.FaceList);
			num3 = Helpers.copyBytes(intPtr, num3, shellfaces.EdgeData);
			num3 = Helpers.copyBytes(intPtr, num3, shellfaces.FaceData);
			return intPtr;
		}

		public static IntPtr MarshalMeshData(MeshData mesh)
		{
			int num = mesh.NumColumns * mesh.NumRows;
			int edgeCount = (mesh.NumColumns - 1) * mesh.NumRows + mesh.NumColumns * (mesh.NumRows - 1);
			int faceCount = (mesh.NumColumns - 1) * (mesh.NumRows - 1);
			int cb = 12 + num * 3 * 8;
			Helpers.VertexDataFields val = (Helpers.VertexDataFields)0;
			Helpers.getVertextDataFlags(mesh.VertexData, num, ref val, ref cb);
			Helpers.getEdgeDataFlags(mesh.EdgeData, edgeCount, ref val, ref cb);
			Helpers.getFaceDataFlags(mesh.FaceData, faceCount, ref val, ref cb);
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			Marshal.WriteInt32(intPtr, mesh.NumRows);
			int num2 = 4;
			Marshal.WriteInt32(intPtr, num2, mesh.NumColumns);
			num2 += 4;
			Marshal.WriteInt32(intPtr, num2, (int)val);
			num2 += 4;
			num2 = Helpers.copyBytes(intPtr, num2, mesh.VertexList);
			num2 = Helpers.copyBytes(intPtr, num2, mesh.VertexData);
			num2 = Helpers.copyBytes(intPtr, num2, mesh.EdgeData);
			num2 = Helpers.copyBytes(intPtr, num2, mesh.FaceData);
			return intPtr;
		}

		public static IntPtr MarshalShellData(ShellData shell)
		{
			int num = shell.Points.Length;
			int faceCount;
			int edgeCount;
			shell.getEdgesFacesCount(out faceCount, out edgeCount);
			int cb = 4 * (3 + shell.Faces.Length) + num * 3 * 8;
			Helpers.VertexDataFields val = (Helpers.VertexDataFields)0;
			Helpers.getVertextDataFlags(shell.VertexData, num, ref val, ref cb);
			Helpers.getEdgeDataFlags(shell.EdgeData, edgeCount, ref val, ref cb);
			Helpers.getFaceDataFlags(shell.FaceData, faceCount, ref val, ref cb);
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			Marshal.WriteInt32(intPtr, shell.Faces.Length);
			int num2 = 4;
			Marshal.WriteInt32(intPtr, num2, shell.Points.Length);
			num2 += 4;
			Marshal.WriteInt32(intPtr, num2, (int)val);
			num2 += 4;
			num2 = Helpers.copyBytes(intPtr, num2, shell.Faces);
			num2 = Helpers.copyBytes(intPtr, num2, shell.Points);
			num2 = Helpers.copyBytes(intPtr, num2, shell.VertexData);
			num2 = Helpers.copyBytes(intPtr, num2, shell.EdgeData);
			num2 = Helpers.copyBytes(intPtr, num2, shell.FaceData);
			return intPtr;
		}

		public static OdGePoint3d[] readPoints(IntPtr data, ref int N, int Len)
		{
			OdGePoint3d[] array = new OdGePoint3d[Len];
			byte[] array2 = new byte[24];
			for (int i = 0; i < Len; i++)
			{
				for (int j = 0; j < 24; j++)
				{
					array2[j] = Marshal.ReadByte(data, N++);
				}
				double xx = BitConverter.ToDouble(array2, 0);
				double yy = BitConverter.ToDouble(array2, 8);
				double zz = BitConverter.ToDouble(array2, 16);
				array[i] = new OdGePoint3d(xx, yy, zz);
			}
			return array;
		}

		public static OdGeVector3d[] readVectors(IntPtr data, ref int N, int Len)
		{
			OdGeVector3d[] array = new OdGeVector3d[Len];
			byte[] array2 = new byte[24];
			for (int i = 0; i < Len; i++)
			{
				for (int j = 0; j < 24; j++)
				{
					array2[j] = Marshal.ReadByte(data, N++);
				}
				double xx = BitConverter.ToDouble(array2, 0);
				double yy = BitConverter.ToDouble(array2, 8);
				double zz = BitConverter.ToDouble(array2, 16);
				array[i] = new OdGeVector3d(xx, yy, zz);
			}
			return array;
		}

		public static OdCmEntityColor[] readTrueColors(IntPtr data, ref int N, int Len)
		{
			OdCmEntityColor[] array = new OdCmEntityColor[Len];
			for (int i = 0; i < Len; i++)
			{
				array[i] = new OdCmEntityColor();
				array[i].setColor((uint)Marshal.ReadInt32(data, N));
				N += 4;
			}
			return array;
		}

		public static ushort[] readColors(IntPtr data, ref int N, int Len)
		{
			ushort[] array = new ushort[Len];
			for (int i = 0; i < Len; i++)
			{
				array[i] = (ushort)Marshal.ReadInt16(data, N);
				N += 2;
			}
			return array;
		}

		public static int[] readIntArray(IntPtr data, ref int N, int Len)
		{
			int[] array = new int[Len];
			for (int i = 0; i < Len; i++)
			{
				array[i] = Marshal.ReadInt32(data, N);
				N += 4;
			}
			return array;
		}

		public static OdDbStub[] readIds(IntPtr data, ref int N, int Len)
		{
			OdDbStub[] array = new OdDbStub[Len];
			for (int i = 0; i < Len; i++)
			{
				IntPtr intPtr = Marshal.ReadIntPtr(data, N);
				N += Marshal.SizeOf(intPtr);
				array[i] = new OdDbStub(intPtr, false);
			}
			return array;
		}

		public static OdGiMapper[] readMappers(IntPtr data, ref int N, int Len)
		{
			OdGiMapper[] array = new OdGiMapper[Len];
			for (int i = 0; i < Len; i++)
			{
				IntPtr intPtr = Marshal.ReadIntPtr(data, N);
				N += Marshal.SizeOf(intPtr);
				array[i] = new OdGiMapper(intPtr, false);
			}
			return array;
		}

		public static OdCmTransparency[] readTransparency(IntPtr data, ref int N, int Len)
		{
			OdCmTransparency[] array = new OdCmTransparency[Len];
			for (int i = 0; i < Len; i++)
			{
				IntPtr intPtr = Marshal.ReadIntPtr(data, N);
				N += Marshal.SizeOf(intPtr);
				array[i] = new OdCmTransparency(intPtr, false);
			}
			return array;
		}

		public static IntPtr[] readIntPtrs(IntPtr data, ref int N, int Len)
		{
			IntPtr[] array = new IntPtr[Len];
			for (int i = 0; i < Len; i++)
			{
				array[i] = Marshal.ReadIntPtr(data, N);
				N += Marshal.SizeOf(array[i]);
			}
			return array;
		}

		public static byte[] readVisibilities(IntPtr data, ref int N, int Len)
		{
			byte[] array = new byte[Len];
			for (int i = 0; i < Len; i++)
			{
				array[i] = Marshal.ReadByte(data, N++);
			}
			return array;
		}

		private static VertexData readVertexData(Helpers.VertexDataFields dataAvailable, int VertexCount, ref int N, IntPtr data)
		{
			VertexData vertexData = null;
			if ((dataAvailable & Helpers.VertexDataFields.HasVertexData) != (Helpers.VertexDataFields)0)
			{
				vertexData = new VertexData();
				vertexData.OrientationFlag = (OdGiOrientationType)Marshal.ReadInt32(data, N);
				N += 4;
				if ((dataAvailable & Helpers.VertexDataFields.HasVertexNormals) != (Helpers.VertexDataFields)0)
				{
					vertexData.Normals = Helpers.readVectors(data, ref N, VertexCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasVertexTrueColors) != (Helpers.VertexDataFields)0)
				{
					vertexData.TrueColors = Helpers.readTrueColors(data, ref N, VertexCount);
				}
			}
			return vertexData;
		}

		private static EdgeData readEdgeData(Helpers.VertexDataFields dataAvailable, int EdgeCount, ref int N, IntPtr data)
		{
			EdgeData edgeData = null;
			if ((dataAvailable & Helpers.VertexDataFields.HasEdgeData) != (Helpers.VertexDataFields)0)
			{
				edgeData = new EdgeData();
				if ((dataAvailable & Helpers.VertexDataFields.HasEdgeColors) != (Helpers.VertexDataFields)0)
				{
					edgeData.Colors = Helpers.readColors(data, ref N, EdgeCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasEdgeLayerIds) != (Helpers.VertexDataFields)0)
				{
					edgeData.LayerIds = Helpers.readIds(data, ref N, EdgeCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasEdgeLinetypeIds) != (Helpers.VertexDataFields)0)
				{
					edgeData.LinetypeIds = Helpers.readIds(data, ref N, EdgeCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasEdgeSelectionMarkers) != (Helpers.VertexDataFields)0)
				{
					edgeData.SelectionMarkers = Helpers.readIntPtrs(data, ref N, EdgeCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasEdgeTrueColors) != (Helpers.VertexDataFields)0)
				{
					edgeData.TrueColors = Helpers.readTrueColors(data, ref N, EdgeCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasEdgeVisibilities) != (Helpers.VertexDataFields)0)
				{
					edgeData.Visibilities = Helpers.readVisibilities(data, ref N, EdgeCount);
				}
			}
			return edgeData;
		}

		private static FaceData readFaceData(Helpers.VertexDataFields dataAvailable, int FaceCount, ref int N, IntPtr data)
		{
			FaceData faceData = null;
			if ((dataAvailable & Helpers.VertexDataFields.HasFaceData) != (Helpers.VertexDataFields)0)
			{
				faceData = new FaceData();
				if ((dataAvailable & Helpers.VertexDataFields.HasFaceColors) != (Helpers.VertexDataFields)0)
				{
					faceData.Colors = Helpers.readColors(data, ref N, FaceCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasFaceLayerIds) != (Helpers.VertexDataFields)0)
				{
					faceData.LayerIds = Helpers.readIds(data, ref N, FaceCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasFaceMappers) != (Helpers.VertexDataFields)0)
				{
					faceData.Mappers = Helpers.readMappers(data, ref N, FaceCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasFaceMaterialIds) != (Helpers.VertexDataFields)0)
				{
					faceData.MaterialIds = Helpers.readIds(data, ref N, FaceCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasFaceNormals) != (Helpers.VertexDataFields)0)
				{
					faceData.Normals = Helpers.readVectors(data, ref N, FaceCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasFaceSelectionMarkers) != (Helpers.VertexDataFields)0)
				{
					faceData.SelectionMarkers = Helpers.readIntPtrs(data, ref N, FaceCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasFaceTransparency) != (Helpers.VertexDataFields)0)
				{
					faceData.Transparency = Helpers.readTransparency(data, ref N, FaceCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasFaceTrueColors) != (Helpers.VertexDataFields)0)
				{
					faceData.TrueColors = Helpers.readTrueColors(data, ref N, FaceCount);
				}
				if ((dataAvailable & Helpers.VertexDataFields.HasFaceVisibilities) != (Helpers.VertexDataFields)0)
				{
					faceData.Visibilities = Helpers.readVisibilities(data, ref N, FaceCount);
				}
			}
			return faceData;
		}

		public static ShellFacesData UnMarshalShellFacesData(IntPtr data)
		{
			ShellFacesData shellFacesData = new ShellFacesData();
			int num = Marshal.ReadInt32(data);
			int num2 = 4;
			Helpers.VertexDataFields dataAvailable = (Helpers.VertexDataFields)Marshal.ReadInt32(data, num2);
			num2 += 4;
			shellFacesData.FaceList = Helpers.readIntArray(data, ref num2, num);
			shellFacesData.EdgeData = Helpers.readEdgeData(dataAvailable, num, ref num2, data);
			shellFacesData.FaceData = Helpers.readFaceData(dataAvailable, num, ref num2, data);
			return shellFacesData;
		}

		public static MeshData UnMarshalFaceData(IntPtr data)
		{
			MeshData meshData = new MeshData();
			meshData.NumColumns = Marshal.ReadInt32(data);
			int num = 4;
			meshData.NumRows = Marshal.ReadInt32(data, num);
			num += 4;
			Helpers.VertexDataFields dataAvailable = (Helpers.VertexDataFields)Marshal.ReadInt32(data, num);
			num += 4;
			int numColumns = meshData.NumColumns;
			int numRows = meshData.NumRows;
			int faceCount = (meshData.NumColumns - 1) * (meshData.NumRows - 1);
			meshData.FaceData = Helpers.readFaceData(dataAvailable, faceCount, ref num, data);
			return meshData;
		}

		public static IntPtr MarshalFaceData(MeshData mesh)
		{
			int numColumns = mesh.NumColumns;
			int numRows = mesh.NumRows;
			int numColumns2 = mesh.NumColumns;
			int numRows2 = mesh.NumRows;
			int numColumns3 = mesh.NumColumns;
			int numRows3 = mesh.NumRows;
			int faceCount = (mesh.NumColumns - 1) * (mesh.NumRows - 1);
			int cb = 12;
			Helpers.VertexDataFields val = (Helpers.VertexDataFields)0;
			Helpers.getFaceDataFlags(mesh.FaceData, faceCount, ref val, ref cb);
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			Marshal.WriteInt32(intPtr, mesh.NumRows);
			int num = 4;
			Marshal.WriteInt32(intPtr, num, mesh.NumColumns);
			num += 4;
			Marshal.WriteInt32(intPtr, num, (int)val);
			num += 4;
			num = Helpers.copyBytes(intPtr, num, mesh.FaceData);
			return intPtr;
		}

		public static MeshData UnMarshalMeshData(IntPtr data)
		{
			MeshData meshData = new MeshData();
			meshData.NumColumns = Marshal.ReadInt32(data);
			int num = 4;
			meshData.NumRows = Marshal.ReadInt32(data, num);
			num += 4;
			Helpers.VertexDataFields dataAvailable = (Helpers.VertexDataFields)Marshal.ReadInt32(data, num);
			num += 4;
			int num2 = meshData.NumColumns * meshData.NumRows;
			int edgeCount = (meshData.NumColumns - 1) * meshData.NumRows + meshData.NumColumns * (meshData.NumRows - 1);
			int faceCount = (meshData.NumColumns - 1) * (meshData.NumRows - 1);
			meshData.VertexList = Helpers.readPoints(data, ref num, num2);
			meshData.VertexData = Helpers.readVertexData(dataAvailable, num2, ref num, data);
			meshData.EdgeData = Helpers.readEdgeData(dataAvailable, edgeCount, ref num, data);
			meshData.FaceData = Helpers.readFaceData(dataAvailable, faceCount, ref num, data);
			return meshData;
		}

		public static ShellData UnMarshalShellData(IntPtr data)
		{
			ShellData shellData = new ShellData();
			int len = Marshal.ReadInt32(data);
			int num = 4;
			int num2 = Marshal.ReadInt32(data, num);
			num += 4;
			Helpers.VertexDataFields dataAvailable = (Helpers.VertexDataFields)Marshal.ReadInt32(data, num);
			num += 4;
			shellData.Faces = Helpers.readIntArray(data, ref num, len);
			shellData.Points = Helpers.readPoints(data, ref num, num2);
			int faceCount;
			int edgeCount;
			shellData.getEdgesFacesCount(out faceCount, out edgeCount);
			shellData.VertexData = Helpers.readVertexData(dataAvailable, num2, ref num, data);
			shellData.EdgeData = Helpers.readEdgeData(dataAvailable, edgeCount, ref num, data);
			shellData.FaceData = Helpers.readFaceData(dataAvailable, faceCount, ref num, data);
			return shellData;
		}

		public static IntPtr MarshalPoint2dFixedArray(OdGePoint2d[] pts, int size)
		{
			if (pts == null)
			{
				return IntPtr.Zero;
			}
			if (pts.Length != size)
			{
				throw new Exception("MarshalPointFixedArray: wrong array length");
			}
			int cb = size * 2 * 8;
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			Helpers.copyBytes(intPtr, 0, pts);
			return intPtr;
		}

		public static OdGePoint2d[] UnMarshalPoint2dFixedArray(IntPtr pts, int size)
		{
			if (pts == IntPtr.Zero)
			{
				return null;
			}
			int num = size * 2 * 8;
			byte[] array = new byte[num];
			Marshal.Copy(pts, array, 0, num);
			OdGePoint2d[] array2 = new OdGePoint2d[size];
			int num2 = 0;
			for (int i = 0; i < size; i++)
			{
				double xx = BitConverter.ToDouble(array, num2);
				num2 += 8;
				double yy = BitConverter.ToDouble(array, num2);
				num2 += 8;
				array2[i] = new OdGePoint2d(xx, yy);
			}
			Marshal.FreeCoTaskMem(pts);
			return array2;
		}

		public static IntPtr MarshalboolFixedArray(bool[] arr)
		{
			if (arr == null)
			{
				return IntPtr.Zero;
			}
			return Helpers.MarshalBoolFixedArray(arr, arr.Length);
		}

		public static IntPtr MarshalBoolFixedArray(bool[] arr, int size)
		{
			if (arr == null)
			{
				return IntPtr.Zero;
			}
			if (arr.Length != size)
			{
				throw new Exception("MarshalBoolFixedArray: wrong array length");
			}
			IntPtr intPtr = Marshal.AllocCoTaskMem(size);
			Helpers.copyBytes(intPtr, 0, arr);
			return intPtr;
		}

		public static bool[] UnMarshalboolFixedArray(IntPtr arr)
		{
			if (IntPtr.Zero == arr)
			{
				return null;
			}
			int size = Marshal.ReadInt32(arr);
			return Helpers.UnMarshalBoolFixedArray(arr, size);
		}

		public static bool[] UnMarshalBoolFixedArray(IntPtr arr, int size)
		{
			if (arr == IntPtr.Zero)
			{
				return null;
			}
			byte[] array = new byte[size];
			Marshal.Copy(arr, array, 0, size);
			bool[] array2 = new bool[size];
			int num = 0;
			for (int i = 0; i < size; i++)
			{
				array2[i] = BitConverter.ToBoolean(array, num);
				num++;
			}
			Marshal.FreeCoTaskMem(arr);
			return array2;
		}

		public static IntPtr MarshalPointFixedArray(OdGePoint3d[] pts, int size)
		{
			if (pts == null)
			{
				return IntPtr.Zero;
			}
			if (pts.Length != size)
			{
				throw new Exception("MarshalPointFixedArray: wrong array length");
			}
			int cb = size * 3 * 8;
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			Helpers.copyBytes(intPtr, 0, pts);
			return intPtr;
		}

		public static OdGePoint3d[] UnMarshalPointFixedArray(IntPtr pts, int size)
		{
			if (pts == IntPtr.Zero)
			{
				return null;
			}
			int num = size * 3 * 8;
			byte[] array = new byte[num];
			Marshal.Copy(pts, array, 0, num);
			OdGePoint3d[] array2 = new OdGePoint3d[size];
			int num2 = 0;
			for (int i = 0; i < size; i++)
			{
				double xx = BitConverter.ToDouble(array, num2);
				num2 += 8;
				double yy = BitConverter.ToDouble(array, num2);
				num2 += 8;
				double zz = BitConverter.ToDouble(array, num2);
				num2 += 8;
				array2[i] = new OdGePoint3d(xx, yy, zz);
			}
			Marshal.FreeCoTaskMem(pts);
			return array2;
		}

		public static IntPtr MarshaldoubleFixedArray(double[] pts)
		{
			if (pts == null)
			{
				return IntPtr.Zero;
			}
			int size = pts.Length;
			return Helpers.MarshalDoubleFixedArray(pts, size);
		}

		public static IntPtr MarshalDoubleFixedArray(double[] pts, int size)
		{
			if (pts == null)
			{
				return IntPtr.Zero;
			}
			if (pts.Length != size)
			{
				throw new Exception("MarshalPointFixedArray: wrong array length");
			}
			int cb = size * 8;
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			int n = 0;
			for (int i = 0; i < size; i++)
			{
				n = Helpers.copyBytes(intPtr, n, BitConverter.GetBytes(pts[i]));
			}
			return intPtr;
		}

		public static double[] UnMarshaldoubleFixedArray(IntPtr pts)
		{
			if (IntPtr.Zero == pts)
			{
				return null;
			}
			int size = Marshal.ReadInt32(pts);
			return Helpers.UnMarshalDoubleFixedArray(pts, size);
		}

		public static double[] UnMarshalDoubleFixedArray(IntPtr pts, int size)
		{
			if (pts == IntPtr.Zero)
			{
				return null;
			}
			int num = size * 8;
			byte[] array = new byte[num];
			Marshal.Copy(pts, array, 0, num);
			double[] array2 = new double[size];
			int num2 = 0;
			for (int i = 0; i < size; i++)
			{
				array2[i] = BitConverter.ToDouble(array, num2);
				num2 += 8;
			}
			return array2;
		}

		public static IntPtr MarshalIntPtrFixedArray(IntPtr[] ptrs)
		{
			if (ptrs == null)
			{
				return IntPtr.Zero;
			}
			int num = ptrs.Length;
			int cb = num * IntPtr.Size;
			IntPtr intPtr = Marshal.AllocCoTaskMem(cb);
			Helpers.copyBytes(intPtr, 0, ptrs);
			return intPtr;
		}

		public static IntPtr[] UnMarshalIntPtrFixedArray(IntPtr arr)
		{
			int num = Marshal.ReadInt32(arr);
			IntPtr[] array = new IntPtr[num];
			for (int i = 0; i < num; i++)
			{
				array[i] = Marshal.ReadIntPtr(arr, 4 + i * IntPtr.Size);
			}
			return array;
		}

		public static IntPtr GetPtr<T>(T[] pts)
		{
			if (pts == null)
			{
				return IntPtr.Zero;
			}
			int cb = 4 + pts.Length * Marshal.SizeOf(typeof(T));
			return Marshal.AllocCoTaskMem(cb);
		}

		public static byte[] GetData<T>(IntPtr pts)
		{
			if (pts == IntPtr.Zero)
			{
				return null;
			}
			int num = Marshal.ReadInt32(pts);
			int num2 = num * Marshal.SizeOf(typeof(T)) + 4;
			byte[] array = new byte[num2];
			Marshal.Copy(pts, array, 0, num2);
			return array;
		}

		public static IntPtr MarshalUInt32FixedArray(uint[] pts)
		{
			IntPtr ptr = Helpers.GetPtr<uint>(pts);
			Marshal.WriteInt32(ptr, pts.Length);
			int n = 4;
			for (int i = 0; i < pts.Length; i++)
			{
				n = Helpers.copyBytes(ptr, n, BitConverter.GetBytes(pts[i]));
			}
			return ptr;
		}

		public static uint[] UnMarshalUInt32FixedArray(IntPtr pts)
		{
			byte[] data = Helpers.GetData<uint>(pts);
			int num = Marshal.ReadInt32(pts);
			uint[] array = new uint[num];
			int num2 = 4;
			for (int i = 0; i < num; i++)
			{
				array[i] = BitConverter.ToUInt32(data, num2);
				num2 += 4;
			}
			return array;
		}

		public static IntPtr MarshalUInt16FixedArray(ushort[] pts)
		{
			IntPtr ptr = Helpers.GetPtr<ushort>(pts);
			Marshal.WriteInt32(ptr, pts.Length);
			int n = 4;
			for (int i = 0; i < pts.Length; i++)
			{
				n = Helpers.copyBytes(ptr, n, BitConverter.GetBytes(pts[i]));
			}
			return ptr;
		}

		public static ushort[] UnMarshalUInt16FixedArray(IntPtr pts)
		{
			byte[] data = Helpers.GetData<ushort>(pts);
			int num = Marshal.ReadInt32(pts);
			ushort[] array = new ushort[num];
			int num2 = 4;
			for (int i = 0; i < num; i++)
			{
				array[i] = BitConverter.ToUInt16(data, num2);
				num2 += 2;
			}
			return array;
		}

		public static IntPtr MarshalUInt64FixedArray(ulong[] pts)
		{
			IntPtr ptr = Helpers.GetPtr<ulong>(pts);
			Marshal.WriteInt32(ptr, pts.Length);
			int n = 4;
			for (int i = 0; i < pts.Length; i++)
			{
				n = Helpers.copyBytes(ptr, n, BitConverter.GetBytes(pts[i]));
			}
			return ptr;
		}

		public static ulong[] UnMarshalUInt64FixedArray(IntPtr pts)
		{
			byte[] data = Helpers.GetData<ulong>(pts);
			int num = Marshal.ReadInt32(pts);
			ulong[] array = new ulong[num];
			int num2 = 4;
			for (int i = 0; i < num; i++)
			{
				array[i] = (ulong)BitConverter.ToUInt32(data, num2);
				num2 += 8;
			}
			return array;
		}

		public static IntPtr MarshalbyteFixedArray(byte[] pts)
		{
			IntPtr ptr = Helpers.GetPtr<byte>(pts);
			Marshal.WriteInt32(ptr, pts.Length);
			int n = 4;
			Helpers.copyBytes(ptr, n, pts);
			return ptr;
		}

		public static byte[] UnMarshalbyteFixedArray(IntPtr pts)
		{
			byte[] data = Helpers.GetData<byte>(pts);
			int num = Marshal.ReadInt32(pts);
			byte[] array = new byte[num];
			int sourceIndex = 4;
			Array.Copy(data, sourceIndex, array, 0, num);
			return array;
		}

		public static IntPtr MarshalInt32FixedArray(int[] pts)
		{
			IntPtr ptr = Helpers.GetPtr<int>(pts);
			Marshal.WriteInt32(ptr, pts.Length);
			int n = 4;
			for (int i = 0; i < pts.Length; i++)
			{
				n = Helpers.copyBytes(ptr, n, BitConverter.GetBytes(pts[i]));
			}
			return ptr;
		}

		public static int[] UnMarshalInt32FixedArray(IntPtr pts)
		{
			byte[] data = Helpers.GetData<int>(pts);
			int num = Marshal.ReadInt32(pts);
			int[] array = new int[num];
			int num2 = 4;
			for (int i = 0; i < num; i++)
			{
				array[i] = BitConverter.ToInt32(data, num2);
				num2 += 4;
			}
			return array;
		}

		public static IntPtr MarshalInt16FixedArray(short[] pts)
		{
			IntPtr ptr = Helpers.GetPtr<short>(pts);
			Marshal.WriteInt32(ptr, pts.Length);
			int n = 4;
			for (int i = 0; i < pts.Length; i++)
			{
				n = Helpers.copyBytes(ptr, n, BitConverter.GetBytes(pts[i]));
			}
			return ptr;
		}

		public static short[] UnMarshalInt16FixedArray(IntPtr pts)
		{
			byte[] data = Helpers.GetData<short>(pts);
			int num = Marshal.ReadInt32(pts);
			short[] array = new short[num];
			int num2 = 4;
			for (int i = 0; i < num; i++)
			{
				array[i] = BitConverter.ToInt16(data, num2);
				num2 += 2;
			}
			return array;
		}

		public static IntPtr MarshalInt64FixedArray(long[] pts)
		{
			IntPtr ptr = Helpers.GetPtr<long>(pts);
			Marshal.WriteInt32(ptr, pts.Length);
			int n = 4;
			for (int i = 0; i < pts.Length; i++)
			{
				n = Helpers.copyBytes(ptr, n, BitConverter.GetBytes(pts[i]));
			}
			return ptr;
		}

		public static long[] UnMarshalInt64FixedArray(IntPtr pts)
		{
			byte[] data = Helpers.GetData<long>(pts);
			int num = Marshal.ReadInt32(pts);
			long[] array = new long[num];
			int num2 = 4;
			for (int i = 0; i < num; i++)
			{
				array[i] = (long)((ulong)BitConverter.ToUInt32(data, num2));
				num2 += 8;
			}
			return array;
		}

		public static IntPtr MarshalVariant(object val)
		{
			string key;
			switch (key = val.GetType().ToString())
			{
			case "System.Boolean":
				return GlobalsPINVOKE.createVariantFromBool((bool)val);
			case "System.Byte":
				return GlobalsPINVOKE.createVariantFromByte((byte)val);
			case "System.Int16":
				return GlobalsPINVOKE.createVariantFromInt16((short)val);
			case "System.UInt16":
				return GlobalsPINVOKE.createVariantFromInt16((short)((ushort)val));
			case "System.Int32":
				return GlobalsPINVOKE.createVariantFromInt32((int)val);
			case "System.UInt32":
				return GlobalsPINVOKE.createVariantFromInt32((int)((uint)val));
			case "System.Int64":
				return GlobalsPINVOKE.createVariantFromInt64((long)val);
			case "System.UInt64":
				return GlobalsPINVOKE.createVariantFromInt64((long)((ulong)val));
			case "System.Double":
				return GlobalsPINVOKE.createVariantFromDouble((double)val);
			case "System.String":
				return GlobalsPINVOKE.createVariantFromString((string)val);
			case "System.IntPtr":
				return GlobalsPINVOKE.createVariantFromIntPtr((IntPtr)val);
			}
			return IntPtr.Zero;
		}

		public static void FreeVariant(IntPtr val)
		{
			GlobalsPINVOKE.deleteVariant(val);
		}

		public static object UnMarshalVariant(IntPtr p)
		{
			switch (GlobalsPINVOKE.getVariantType(p))
			{
			case 1:
				return GlobalsPINVOKE.getStringFromVariant(p);
			case 2:
				return GlobalsPINVOKE.getBoolFromVariant(p);
			case 3:
				return GlobalsPINVOKE.getByteFromVariant(p);
			case 4:
				return GlobalsPINVOKE.getInt16FromVariant(p);
			case 5:
				return GlobalsPINVOKE.getInt32FromVariant(p);
			case 6:
				return GlobalsPINVOKE.getInt64FromVariant(p);
			case 7:
				return GlobalsPINVOKE.getDoubleFromVariant(p);
			case 8:
				return GlobalsPINVOKE.getAnsiStringFromVariant(p);
			default:
				return null;
			}
		}

		public static uint[] UnMarshalPalette(IntPtr p)
		{
			if (p == IntPtr.Zero)
			{
				return null;
			}
			int num = Marshal.ReadInt32(p);
			uint[] array = new uint[num];
			for (int i = 1; i < num + 1; i++)
			{
				array[i - 1] = (uint)Marshal.ReadInt32(p, i * 4);
			}
			return array;
		}

		public static IntPtr MarshalPalette(uint[] p)
		{
			if (p == null)
			{
				return IntPtr.Zero;
			}
			IntPtr intPtr = Marshal.AllocCoTaskMem(4 + p.Length * 4);
			Marshal.WriteInt32(intPtr, p.Length);
			for (int i = 1; i < p.Length + 1; i++)
			{
				Marshal.WriteInt32(intPtr, i * 4, (int)p[i - 1]);
			}
			return intPtr;
		}

		private static Dictionary<IntPtr, Helpers.RxObjectDynamicCreatorDelegate> constructorMap = new Dictionary<IntPtr, Helpers.RxObjectDynamicCreatorDelegate>();

		private delegate OdRxObject RxObjectDynamicCreatorDelegate(IntPtr nativePtr, bool own);

		[Flags]
		private enum VertexDataFields
		{
			HasVertexData = 1,
			HasFaceData = 2,
			HasEdgeData = 4,
			HasEdgeColors = 8,
			HasEdgeTrueColors = 16,
			HasEdgeLayerIds = 32,
			HasEdgeLinetypeIds = 64,
			HasEdgeSelectionMarkers = 128,
			HasEdgeVisibilities = 256,
			HasFaceColors = 512,
			HasFaceTrueColors = 1024,
			HasFaceLayerIds = 2048,
			HasFaceSelectionMarkers = 4096,
			HasFaceVisibilities = 8192,
			HasFaceNormals = 16384,
			HasFaceMaterialIds = 32768,
			HasFaceMappers = 65536,
			HasFaceTransparency = 131072,
			HasVertexNormals = 262144,
			HasVertexTrueColors = 524288
		}
	}
}
